/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xo;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author william
 */
public class XOObject extends XOBase<Map<String, Object>> implements IXOObject {

    public XOObject() {
        m = new HashMap<String, Object>();
    }

    public XOObject(Map m) {
        this.m = m;
    }

    @Override
    public int _int(String key) {
        return (Integer) m.get(key);
    }

    @Override
    public long _long(String key) {
        return (Long) m.get(key);
    }

    @Override
    public double _double(String key) {
        return (Double) m.get(key);
    }

    @Override
    public Date _date(String key) {
        return (Date) m.get(key);
    }

    @Override
    public IXOObject _xo(String key) {
        return (IXOObject) m.get(key);
    }

    @Override
    public IXOArray _xa(String key) {
        return (IXOArray) m.get(key);
    }

    @Override
    public Object _set(String key, Object value) {
        return m.put(key, value);
    }

    @Override
    public Object check(String key, String dt, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public IXOObject _clone() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int _length() {
        return m.size();
    }

    @Override
    public int asInt(String key, int dv) {
        Object o = m.get(key);
        if (o instanceof Number) {
            return ((Number) o).intValue();
        } else if (o instanceof String) {
            String s = (String) o;
            return (s.length() > 0) ? Integer.parseInt(s) : dv;
        }
        return dv;
    }

    @Override
    public long asLong(String key, long dv) {
        Object o = m.get(key);
        if (o instanceof Number) {
            return ((Number) o).longValue();
        } else if (o instanceof String) {
            String s = (String) o;
            return (s.length() > 0) ? Long.parseLong(s) : dv;
        }
        return dv;
    }

    @Override
    public double asDouble(String key, double dv) {
        Object o = m.get(key);
        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        } else if (o instanceof String) {
            String s = (String) o;
            return (s.length() > 0) ? Double.parseDouble(s) : dv;
        }
        return dv;
    }

    @Override
    public Date asDate(String key, Date dv) {
        Object o = m.get(key);
        if (o instanceof Date) {
            return (Date) o;
        } else if (o instanceof String) {
            try {
                return xo_date_fmt.parse((String) o);
            } catch (ParseException ex) {
                log.error("Can't cast to Date : " + o, ex);
                return dv;
            }
        }
        return dv;
    }

    @Override
    public String asString(String key) {
        Object o = m.get(key);
        return (o != null) ? o.toString() : "";
    }

    @Override
    public Object obj(String key) {
        return m.get(key);
    }

    @Override
    public IXOObject asXO(String key) {
        Object o = m.get(key);
        if (o instanceof IXOObject) {
            return (IXOObject) o;
        }
        return null;
    }

    @Override
    public IXOArray asXA(String key) {
        Object o = m.get(key);
        if (o instanceof IXOArray) {
            return (IXOArray) o;
        }
        return null;
    }

    @Override
    public String json() {
        try {
            StringBuilder sb = new StringBuilder("{");
            Iterator<String> keys = m.keySet().iterator();
            while (keys.hasNext()) {
                if (sb.length() > 1) {
                    sb.append(',');
                }
                String key = keys.next();
                sb.append(quote(key));
                sb.append(':');
                sb.append(valueToString(obj(key)));
            }
            sb.append('}');
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
